<template>
  <div class="trend-container">
    <!-- 标题区域 -->
    <div class="title" :style="titleStyle">
      <span>{{ '┃ ' + showTitle }}</span>
      <!-- 引入字体文件 -->
      <span class="iconfont title-icon" @click="isShow = !isShow" :style="titleStyle">&#xe6eb;</span>
      <div class="title-select" v-show="isShow" :style="marginStyle">
        <div 
          v-for="item in selectTypes" 
          :key="item.key"
          @click="handleSelect(item.key)">{{ item.text }}
        </div>
      </div>
    </div>
    <!-- 图表区域 -->
    <div class="trend-chart" ref="chartRef"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '../utils/theme_utils.js'

export default {
  name:'Trend',
  data() {
    return {
      chartInstance: null,
      allData: null,
      isShow: false,
      type: 'map', // 不同数据的类型
      titleFontSize: 0, // 标题的字体大小
    }
  },
  computed: {
    selectTypes() {
      if(!this.allData) {
        return []
      } else {
        return this.allData.type.filter(item => {
          return item.key !== this.type
        })
      }
    },
    showTitle() {
      if(!this.allData) {
        return ''
      } else {
        return this.allData[this.type].title
      }
    },
    // 标题的样式
    titleStyle() {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize + 'px',
        background: getThemeValue(this.theme).backgroundColor
      }
    },
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // 主题改变时，销毁当前图表
      this.chartInstance.dispose()
      // 以最新的主题初始化图表
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  },
  created() {
    // 组件创建完成后进行回调函数的注册
    this.$socket.registerCallBack('trendData', this.getData)
  },
  mounted() {
    this.initChart()
    // this.getData()
    // 发送数据给服务器,让服务器返回需要的数据
     this.$socket.send({
       action: 'getData',
       socketType: 'trendData',
       chartName: 'trend',
       value: ''
     })
    window.addEventListener('resize',this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize',this.screenAdapter)
    // 组件销毁时进行回调函数的注销
    this.$socket.unRegisterCallBack('trendData')
  },
  methods: {
    // 初始化echarts实例对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chartRef, this.theme)
      const initOption = {
        grid: {
          left: '3%',
          right: '4%',
          top: '35%',
          bottom: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          left: 20,
          top:'15%',
          icon: 'circle'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false, // 是否设置与y轴间隔
        },
        yAxis: {
          type: 'value'
        }
      }
      this.chartInstance.setOption(initOption)
    },

    // 获取图表的数据, result参数是服务端发送给客户端的数据
    getData(result) {
      // const result = await this.$http.get('trend')
      this.allData = result
      this.updateChart()
    },

    // 更新图表
    updateChart() {
      const colorArr1 = [
        'rgba(44,110,255,0.5)',
        'rgba(22,242,217,0.5)',   
        'rgba(254,33,30,0.5)',
        'rgba(250,105,0,0.5)'
      ]
      const colorArr2 = [
        "rgba(44,110,255,0)",
        "rgba(22,242,217,0)",   
        "rgba(254,33,30,0)",
        "rgba(250,105,0,0)"
      ]
      // 类目轴
      const timeArr = this.allData.common.month
      // y轴
      const valueArr = this.allData[this.type].data
      // series数据
      const seriesArr = valueArr.map((item,index) => {
        return {
          name: item.name,
          type: 'line',
          data: item.data,
          stack: this.type, // 设置堆叠图
          // 设置区域填充样式
          areaStyle: {
            opacity: 0.3,
            colorStops: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1,[
              {
                offset: 0,
                color: colorArr1[index]
              },
              {
                offset: 1,
                color: colorArr2[index]
              }
            ])
          },
        }
      })
      // 图例的数据
      const legendArr = valueArr.map(item => {
        return item.name
      })
      const dataOption = {
        xAxis: {
          data: timeArr
        },
        series: seriesArr,
        legend: {
          data: legendArr
        },
      }
      this.chartInstance.setOption(dataOption)
    },

    // 屏幕适配
    screenAdapter() {
      this.titleFontSize = this.$refs.chartRef.offsetWidth / 100 * 3.6
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize, // 图例的间距
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },

    // 不同数据的选择
    handleSelect(currentType) {
      this.type = currentType
      this.updateChart()
      this.isShow = false
    }
  },
}
</script>

<style scoped>
.title {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 99;
  color: #fff;
}
.title-icon {
  margin-left: 8px;
  font-size: 17px;
  cursor: pointer;
}
</style>